<?php
class AdminAction extends PiscesAction
{
	public function __construct()
	{
		parent::__construct();
	}

	public function beforeExecute()
	{
		parent::beforeExecute();
		
		$this->data['site']		= C('SettingService')->get('site_setting',true);
		$apiSetting				= C('SettingService')->get('api_setting',true);
		$this->data['taobao']['pid']	= $apiSetting['taobao_api_pid'];
		$this->data['config']	= C('SettingService')->get('config_setting',true);
		
		$this->context->adminId 	= 0;
		$authCode 					= C('LtCookie')->getCookie('auth');
		if ($authCode)
		{
			list($id, $password, $ip) = explode("\t", $authCode);
			$adminService	= new AdminService();
			$user 			= $adminService->get($id);
			if ($user && $user['password'] == $password && (!$this->data['config']['system']['ipcheck'] || $ip==$this->getIp()))
			{
				$this->data['user'] 	= $user;
				$this->data['id'] 		= $user['id'];
				$this->context->adminId	= $user['id'];
			}
			else
			{
				$this->data['username'] = '';
				$this->data['id'] 		= 0;
				C('LtCookie')->delCookie('auth');
			}
		}
		if (!$this->data['id'])
		{
			if($this->responseType=='json')
			{
				$this->data['data']['loginUrl']	= C('LtUrl')->generate('Default', 'Login');
				$this->writeResponse();
				exit;
			}
			else
			{
				header("Location: " . C('LtUrl')->generate('Default', 'Login'));
				exit;
			}
		}
	}
}
